package com.alan.controller;

import com.alan.entity.teacherstudent;
import com.alan.repository.StudentRepository;
import com.alan.repository.TeacherRepository;
import com.alan.repository.impl.StudentRepositoryImpl;
import com.alan.repository.impl.TeacherRepositoryImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/teacherstudent")
public class TeacherStudentServlet extends HttpServlet {
    private StudentRepository studentRepository = new StudentRepositoryImpl();
    private TeacherRepository teacherRepository = new TeacherRepositoryImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String pagestr=req.getParameter("page");
        Integer page=Integer.parseInt(pagestr);
        session.setAttribute("page",page);
        String method = req.getParameter("method");
        switch (method) {
            case "change":
                String name2 = req.getParameter("stu_id");
                teacherstudent student1 = studentRepository.find(name2);
                req.setAttribute("student", student1);
                req.getRequestDispatcher("/teacher/changestudentmarks.jsp").forward(req, resp);
                break;
            case "update":
                String name = req.getParameter("name");
                teacherstudent student = studentRepository.find(name);
                req.setAttribute("student", student);
                req.getRequestDispatcher("/teacher/Update.jsp").forward(req, resp);
                break;
            case "delete":
                String name1 = req.getParameter("name");
                teacherRepository.delete(name1);
                resp.sendRedirect("/teacher?method=find&page="+page);
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Integer page=(Integer) session.getAttribute("page");
        String method = req.getParameter("method");
        switch (method) {
            case "change":
                String stu_id = req.getParameter("name");
                String Mmarksstr = req.getParameter("Mmarks");
                String Emarksstr = req.getParameter("Emarks");
                Double Mmarks = Double.parseDouble(Mmarksstr);
                Double Emarks = Double.parseDouble(Emarksstr);
                studentRepository.changemarks(stu_id, Mmarks, Emarks);
                resp.sendRedirect("/teacher?method=marks&page="+page);
                break;
            case "update":
                stu_id = req.getParameter("stu_id");
                String Cname = req.getParameter("Cname");
                String Ename = req.getParameter("Ename");
                String Ntel = req.getParameter("Ntel");
                String Nclass = req.getParameter("Nclass");
                String semesterstr = req.getParameter("semester");
                Integer semster = Integer.parseInt(semesterstr);
                teacherRepository.update(stu_id, Cname, Ename, Ntel, Nclass, semster);
                resp.sendRedirect("/teacher?method=find&page="+page);
                break;
        }
    }
}
